package com.unionbigdata.teaching.common;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {
    public static final long serialVersionUID = 42L;
    public static final String SUCCESS_CODE = "200";
    public static final String FAIL_CODE = "500";

    public static final String SUCCESS_MSG = "success";
    public static final String FAIL_MSG = "fail";

    private String code;

    private String msg;
    private T content;
    private String requestId;
    /**
     * 先保留原id，避免其他版本报错
     */
    private String id;

    public static Result ok() {
        Result r = new Result(Result.SUCCESS_CODE, Result.SUCCESS_MSG);
        return r;
    }

    public static <T> Result<T> ok(T content) {
        Result r = new Result(Result.SUCCESS_CODE, SUCCESS_MSG);
        r.setContent(content);
        return r;
    }

    public static Result fail() {
        Result r = new Result(Result.FAIL_CODE, Result.FAIL_MSG);
        return r;
    }

    public static Result fail(String msg) {
        Result r = new Result(Result.FAIL_CODE, msg);
        return r;
    }

    public static Result error(String code, String msg) {
        Result r = new Result(code, msg);
        return r;
    }

    public static Result error(String id, String code, String msg) {
        Result r = new Result(id, code, msg);
        return r;
    }

    public Result() {
    }

    public Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(String id, String code, String msg) {
        this.requestId = id;
        this.code = code;
        this.msg = msg;
    }

    public Result(T content) {
        this.code = SUCCESS_CODE;
        this.msg = Result.SUCCESS_MSG;
        this.content = content;
    }

    public boolean success() {
        return SUCCESS_CODE.equals(this.code);
    }

}